package by.bucha.testproject.service.handlers.impl;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.ResultReceiver;
import by.bucha.testproject.content.ApplicationProvider;
import by.bucha.testproject.service.ApplicationService;
import by.bucha.testproject.service.handlers.BaseHandler;

public class DbInsertHandler extends BaseHandler {

	@Override
	public void doExecute(final Intent intent, final Context context,
			ResultReceiver callback) {

		new Thread(new Runnable() {
			@Override
			public void run() {
				Uri uri = Uri.parse(intent
						.getStringExtra(ApplicationService.EXTRA_URI));

				String modelToInsert = intent
						.getStringExtra(ApplicationService.EXTRA_SERIALIZED_MODEL);

				ContentValues contentValues = new ContentValues();
				contentValues
						.put(ApplicationProvider.EXTRA_DATA, modelToInsert);

				contentValues.put(ApplicationProvider.EXTRA_MODEL_TYPE, intent
						.getStringExtra(ApplicationService.EXTRA_MODEL_TYPE));

				String url = context.getContentResolver()
						.insert(uri, contentValues).toString();

				Bundle bundle = new Bundle();
				bundle.putString(ApplicationService.EXTRA_CREATED_ROW_URL, url);

				sendUpdate(SUCCESS_RESPONSE, bundle);
			}
		}).start();
	}

}
